﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MobileAPI.Web.Data
{
    /// <summary>
    /// 移动端发送的通信消息
    /// </summary>
    public class MobileReqMsg : BaseMobileMsg
    {
        /// <summary>
        /// 验证移动端发送的数据，并将其转化为 MobileReqMsg 对象
        /// </summary>
        /// <param name="msg">验证移动端发送的数据</param>
        /// <returns></returns>
        public static MobileReqMsg VerifyData(string msg)
        {
            if (string.IsNullOrEmpty(msg))
            {
                return null;
            }

            MobileReqMsg req = new MobileReqMsg();
            try
            {
                if (msg.Trim().StartsWith("{"))
                {
                    req.FromJson(msg);
                }
                else
                {
                    req.FromXml(msg);
                }

                if (string.IsNullOrEmpty(req.Head.APICode.Trim()))
                {
                    req.Head.RspCode = MessageCode.APICode_Empty.Code;
                    req.Head.RspMsg = MessageCode.APICode_Empty.Msg;
                    return req;
                }

                req.Head.RspCode = MessageCode.Success.Code;
                req.Head.RspMsg = MessageCode.Success.Msg;
            }
            catch (Exception)
            {
                req.Head.RspCode = MessageCode.Format_Error.Code;
                req.Head.RspMsg = MessageCode.Format_Error.Msg;
            }
            return req;
        }
    }
}